<?php

// UPDATE: Please note that these functions are just examples of things that 
// can be done, nothing more.

// Translation data support functions that are called by entries in 
// GMLP_DEF.PHP -- this file gets included by it. See file GMLP.TXT.
// All functions simply modify a string in some manner (short or very 
// long with newlines; strings are strings though)

function gmlp_convertcase($s) {
	$s = ucwords(strtolower($s));
// simple:
//	return '<h2>'.$s.'</h2>';
// link like:
	$sp = '<span id="'.str_replace(' ','_',$s).'"></span>';
	return $sp.'<h2>'.$s.'</h2>';
}

function gmlp_highlightstr($s) {
	return highlight_string(rtrim($s),TRUE);
}

// highlighed block without the PHP start/end tags (need to add and then 
// remove)

function gmlp_highlightstr_php($s) {
	$s = "<?php\n$s\n?>";
	$s = highlight_string($s,TRUE);

	// remove the start/end tags
	$s = str_replace('&lt;?php<br />','',$s);
	// (highlight_string() can end differently)
	$c = ini_get('highlight.default');
	if (preg_match("/$c\">\?&gt;<\/span>\n/",$s))
		$replace = "<span style=\"color: $c\">?&gt;</span>\n";
	else
		$replace = '<br />?&gt;';
	$s = str_replace($replace,'',$s);

	// remove some stuff not needed and reduce size
	$s = str_replace("\n",'',$s);
	$c = ini_get('highlight.html');
	$s = str_replace("<code><span style=\"color: $c\">",'<code>',$s);
	$s = str_replace('</span></code>','</code>',$s);
	$s = str_replace('&nbsp;&nbsp;','&nbsp; ',$s);

	return $s;
}

// these are so simple they indicate that a form of string substitution, 
// or the use of a RE, can be applied directly, thereby moving even more 
// string literala "into the data"; the mechanism to do this is not 
// exactly apparent to me though...

function gmlp_list($s) {
	return '<li>'.$s;
}

function gmlp_dlist($s) {
	return preg_replace('/(\S+)(\s+)(.*)/','<dt><tt>$1</tt><dd>$3</dd>',$s);
}

function gmlp_pre($s) {
	return '<pre>'.$s.'</pre>';
}

// "What would Geshi do?"

function gmlp_ini($s) {
	$s = htmlentities($s);

	if (preg_match('/^\s*;/',$s))
		$s = "<span style=\"color:#666;\">$s</span>";
	else
	if (preg_match('/^\s*\[/',$s))
		$s = "<span style=\"color:#000066;\">$s</span>";
	else
	if (preg_match('/(\s*)([a-z]+)\s*=\s*(.*)/',$s,$res)) {
		$s = "{$res[1]}<span style=\"color:#000099;\">{$res[2]}</span>";
		$s.= " <span style=\"color:#000066;\">=</span>";
		if (isset($res[3]))
			$s.= " <span style=\"color:#660066;\">{$res[3]}</span>";
	}
	return $s;
}

/*
How to make the code appear to be "smart" by highlighting PHP function 
calls and strings embedded in text:

       $2		special PHP PCRE variables
       $foo		PHP variable
       foobar()		PHP function calls
*/

function gmlp_php_string($s) {

	$s = gmlp_highlightstr_php($s);
	// remove more stuff not needed
	$s = str_replace('<br />','',$s);
	// to avoid some inline translations
	$s = str_replace('\'','&#39;',$s);
	$s = str_replace('$','&#36;',$s);

	return $s;
}

// a more complicated version of gmlp_highlightstr_php()

function gmlp_div_highlightstr_php($s) {
	$s = "<?php\n$s?>";
	$s = highlight_string($s,TRUE);

	$s = str_replace('&lt;?php<br />','',$s);
	// highlight_string() can end differently
	$c = ini_get('highlight.default');
	if (preg_match("/$c\">\?&gt;<\/span>\n/",$s))
		$replace = "<span style=\"color: $c\">?&gt;</span>\n";
	else
		$replace = '<br />?&gt;';
	$s = str_replace($replace,'',$s);

	// remove not needed stuff and reduce size 
	$c = ini_get('highlight.html');
	$s = str_replace("\r",'',$s);		// see note EOF
	$s = str_replace("\n",'',$s);

	$s = str_replace("<code><span style=\"color: $c\">",'',$s);
	$s = str_replace('</span></span></code>','</span>',$s);
	$s = str_replace('color: ','color:',$s);
	$s = str_replace('&nbsp;&nbsp;','&nbsp; ',$s);

	// prepare final output
	$s = str_replace('<br />','<br><span class="n"></span>',$s);
	$s = '<div class="php"><span title="highlighting the code will '.
	'exclude the line numbers">PHP code</span>'."\n".'<span class="n">'.
	'</span>'.$s."</div>\n";
	return $s;
/*
   There is a drawback to this function as the SPAN to create line numbers 
   is within a SPAN, making long lines "screw up" the displaying if the 
   enclosing DIV is not wide enough for them. A fix may happen. (Geshi 
   results are more conducive to line number formatting by modifying the 
   output like this. Unfortunately, Geshi is too slow, to me, for lengthy 
   source code files online.)
*/
}

/* note
It may just be the Windows PHP (version 5.3.5) implementation (I have yet 
to confirm due to a computer crash), but highlight_string() can have 
(exasperatingly) some (oddly placed) CRs within it (dang).
*/
